/// 首页评论模型
class HomeComment {
  final int commentId;           // 评论ID
  final String commentTitle;     // 评论标题
  final String commentContent;   // 评论内容
  final String publisherNickname; // 发布者昵称
  final int likeCount;           // 点赞数
  final bool isLiked;            // 是否已点赞
  final String? publisherAvatar; // 发布者头像
  final String? publishTime;     // 发布时间
  final List<String>? images;    // 评论图片列表
  final int? shopId;             // 关联商店ID
  final double? rating;          // 评分

  HomeComment({
    required this.commentId,
    required this.commentTitle,
    required this.commentContent,
    required this.publisherNickname,
    required this.likeCount,
    required this.isLiked,
    this.publisherAvatar,
    this.publishTime,
    this.images,
    this.shopId,
    this.rating,
  });

  Map<String, dynamic> toJson() {
    return {
      'commentId': commentId,
      'commentTitle': commentTitle,
      'commentContent': commentContent,
      'publisherNickname': publisherNickname,
      'likeCount': likeCount,
      'isLiked': isLiked,
      if (publisherAvatar != null) 'publisherAvatar': publisherAvatar,
      if (publishTime != null) 'publishTime': publishTime,
      if (images != null) 'images': images,
      if (shopId != null) 'shopId': shopId,
      if (rating != null) 'rating': rating,
    };
  }

  factory HomeComment.fromJson(Map<String, dynamic> json) {
    return HomeComment(
      commentId: json['commentId'] as int,
      commentTitle: json['commentTitle'] as String,
      commentContent: json['commentContent'] as String,
      publisherNickname: json['publisherNickname'] as String,
      likeCount: json['likeCount'] as int,
      isLiked: json['isLiked'] as bool,
      publisherAvatar: json['publisherAvatar'] as String?,
      publishTime: json['publishTime'] as String?,
      images: json['images'] != null ? List<String>.from(json['images']) : null,
      shopId: json['shopId'] as int?,
      rating: json['rating'] != null ? (json['rating'] as num).toDouble() : null,
    );
  }

  /// 创建点赞后的副本
  HomeComment copyWithLike({required bool isLiked, required int likeCount}) {
    return HomeComment(
      commentId: commentId,
      commentTitle: commentTitle,
      commentContent: commentContent,
      publisherNickname: publisherNickname,
      likeCount: likeCount,
      isLiked: isLiked,
      publisherAvatar: publisherAvatar,
      publishTime: publishTime,
      images: images,
      shopId: shopId,
      rating: rating,
    );
  }

  @override
  String toString() {
    return 'HomeComment(commentId: $commentId, commentTitle: $commentTitle, publisherNickname: $publisherNickname, likeCount: $likeCount, isLiked: $isLiked)';
  }
}
